package com.graduation.roombooking.controller.web;

import com.graduation.roombooking.service.IEquipmentService;
import com.graduation.roombooking.service.UserService;
import com.graduation.roombooking.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author:CaiShuangLian
 * @FileName:
 * @Date:Created in  2022/5/4 19:12
 * @Version:
 * @Description:TODO
 */
@RestController
@RequestMapping("/web/equipment")
@Api(tags = "web——设备模块")
public class WebEquipmentController {
    @Autowired
    private IEquipmentService equipmentService;
    @Autowired
    private UserService userService;

    @ApiOperation("获取设备列表")
    @ResponseBody
    @PostMapping("/list")
    public RespBean getList(@RequestBody String ticket, HttpServletRequest request, HttpServletResponse response){
        if(ticket!=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null ){
                if(userVo.getAction().equals("system_administrator")) {
                    return equipmentService.getList(null);
                }else if(userVo.getAction().equals("organization_administrator")||userVo.getAction().equals("organization_member")){
                    return equipmentService.getList(userVo.getCompanyName());
                }
            }
            return RespBean.error(RespBeanEnum.NO_OPERATION_PERMISSION,"非系统管理员，无法查看");
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

    @ApiOperation("添加设备")
    @ResponseBody
    @PostMapping("/add")
    public RespBean addOne(@RequestBody AddEquipmentVo addEquipmentVo,HttpServletRequest request, HttpServletResponse response){
        String ticket=addEquipmentVo.getTicket();
        if(ticket!=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null ){
                if(userVo.getAction().equals("organization_administrator")) {
                    return equipmentService.addOne(addEquipmentVo,userVo.getCompanyName());
                }
            }
            return RespBean.error(RespBeanEnum.NO_OPERATION_PERMISSION,"非系统管理员，无法添加");
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

    @ApiOperation("修改设备信息")
    @ResponseBody
    @PostMapping("/update")
    public RespBean updateEquipment(@RequestBody UpdateEquipmentVo updateEquipmentVo,HttpServletRequest request, HttpServletResponse response){
        String ticket=updateEquipmentVo.getTicket();
        if(ticket!=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null ){
                if(userVo.getAction().equals("organization_administrator")) {
                    return equipmentService.updateEquipment(updateEquipmentVo,userVo.getCompanyName());
                }
            }
            return RespBean.error(RespBeanEnum.NO_OPERATION_PERMISSION,"非系统管理员，无法添加");
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

    @ApiOperation("删除设备信息")
    @ResponseBody
    @PostMapping("/delete")
    public RespBean deleteOne(@RequestBody DeleteVo deleteVo,HttpServletRequest request, HttpServletResponse response){
        String ticket=deleteVo.getTicket();
        if(ticket!=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null ){
                if(userVo.getAction().equals("organization_administrator")) {
                    return equipmentService.deleteOne(deleteVo.getId(),userVo.getCompanyName());
                }
            }
            return RespBean.error(RespBeanEnum.NO_OPERATION_PERMISSION,"非系统管理员，无法添加");
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

    @ApiOperation("联合查询模糊查询")
    @ResponseBody
    @PostMapping("/union/query")
    public RespBean unionQuery(@RequestBody EquipmentUnionQueryVo unionQueryVo,HttpServletRequest request, HttpServletResponse response){
        String ticket=unionQueryVo.getTicket();
        if(ticket!=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo.getAction().equals("system_administrator")) {
                return equipmentService.unionQuery(unionQueryVo,null);
            }else if(userVo.getAction().equals("organization_administrator")||userVo.getAction().equals("organization_member")){
                return equipmentService.unionQuery(unionQueryVo,userVo.getCompanyName());
            }
            return RespBean.error(RespBeanEnum.NO_OPERATION_PERMISSION,"非系统管理员，无法查看");
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }



}
